package com.projectframe.base.ui;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public abstract class BaseAction extends ActionSupport implements ServletRequestAware,
        ServletResponseAware, ApplicationContextAware, ServletContextAware, SessionAware
{
    protected Logger              LOG     = Logger.getLogger(this.getClass());
    @Autowired
    protected MessageSource       messageSource;
    protected HttpSession         session;
    protected HttpServletRequest  request;
    protected HttpServletResponse response;
    protected ServletContext      servletContext;
    protected ApplicationContext  applicationContext;
    protected Map<String, Object> sessionMap;
    //
    protected int                 status  = 0;                                // 状态
    protected String              message = "";                               // 错误消息
    protected String              msgDesc = "";                               // 错误详细信息
    private BaseForm              form;
    
    public abstract BaseForm createForm();
    
    public void setErrorMessage(final int status, final String message, final String msgDesc)
    {
        this.status = status;
        this.message = message;
        this.msgDesc = msgDesc;
    }
    
    public int getStatus()
    {
        return status;
    }

    public void setStatus(int status)
    {
        this.status = status;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage(String message)
    {
        this.message = message;
    }

    public String getMsgDesc()
    {
        return msgDesc;
    }

    public void setMsgDesc(String msgDesc)
    {
        this.msgDesc = msgDesc;
    }

    public BaseForm getForm()
    {
        if(form==null)
            form = createForm();
        
        return form;
    }

    public void setForm(BaseForm form)
    {
        this.form = form;
    }
    
    @SuppressWarnings("unchecked")
    public <T> T actionForm()
    {
        return (T)getForm();
    }
    
    //=======================================================================
    //=======================================================================
    //=======================================================================
    //=======================================================================

    public void setApplicationContext(ApplicationContext ctx)
    {
        this.applicationContext = ctx;
    }

    public void setServletRequest(HttpServletRequest request)
    {
        this.session = request.getSession();
        this.request = request;
    }

    public void setServletResponse(HttpServletResponse response)
    {
        this.response = response;
    }

    public void setServletContext(ServletContext context)
    {
        this.servletContext = context;
    }

    public void setSession(Map<String, Object> session)
    {
        this.sessionMap = session;
    }
    
    // public Object getPrincipal()
    // {
    // Object securityObj =
    // SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    // return securityObj;
    // }
}
